// pages/addUser/addUser.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pictureUrl: '/image/',
    userList:[],
    chooseFlag:false,
    idx:0,
    deleteId:null,
    deleteFlag:false
  },
  add(){
    wx.showLoading({
      title: '等待中',
      mask:true
    })
    wx.navigateTo({
      url: '../editUser/editUser',
    },wx.hideLoading())
  },
  // 点击确定
  btn(){
    if(this.data.userList.length !== 0){
        let msg = JSON.parse(this.data.query);
        msg.ipid=this.data.userList[this.data.idx].id
        msg.census_type = this.data.userList[this.data.idx].census_type
        // console.log(msg)
        let query = JSON.stringify(msg)
        wx.showLoading({
          title: '等待中',
          mask:true
        })
        if (this.data.fromto =='social'){
          if (msg.city_code=='other'){
            wx.navigateTo({
              url: `../other/other?query=${query}`,
            },wx.hideLoading())
          } else if (msg.city_code =='bu'){
            wx.navigateTo({
              url: `../other/other?query=${query}`,
            }, wx.hideLoading())
          } else{
            app.request('daijiao/order/shebao_save', msg, true).then(res => {
              wx.hideLoading()
              if (res.error_code == 0) {
                wx.navigateTo({
                  url: `../submitOrder/submitOrder?orderid=${res.data.order_id}`,
                })
              } else {
                wx.showModal({
                  content: res.error_message,
                  showCancel: false
                })
              }
            })

            // wx.navigateTo({
            //   // url: `../socialDetail/socialDetail?query=${query}`
            //   url:'../submitOrder/submitOrder'
            // },wx.hideLoading())
          }
        }else{
          wx.navigateTo({
            url: `../goldDetail/goldDetail?query=${query}`,
          },wx.hideLoading())
        }
    }else{
      wx.showModal({
        content: '您还没有选择参保人',
        showCancel:false
      })
    }
  },
  // 点击删除
  deleteUser(e){
    let id = e.currentTarget.dataset.id;
    this.setData({
      deleteId:id,
      deleteFlag:true
    })
  },
  comfirmDelete(){
    app.request("daijiao/insured_person/delete",{ipid:this.data.deleteId},true).then((res)=>{
      if(res.error_code==0){
      let arr=this.data.userList.filter(item=>item.id!==this.data.deleteId)
      this.setData({
        userList:arr,
        deleteFlag:false
      }, wx.showToast({
        title: '删除成功',
      }))
      }else{
        wx.showModal({
          content: res.error_message,
          showCancel:false
        })
      }
    })
  },
  // 编辑参保人
  editUser(e){
    let id = e.currentTarget.dataset.id;
    wx.showLoading({
      title: '等待中',
      mask:true
    })
    console.log(id)
    wx.navigateTo({
      url: `../editUser/editUser?id=${id}`,
    },wx.hideLoading())
  },
  // 选择参保人
  choose(e){
    let idx=e.currentTarget.dataset.idx
    this.setData({
      idx:idx
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.query){
      this.setData({
        query:options.query
      })
    }
    if (options.fromto){
      this.setData({
        fromto:options.fromto
      })
    }
  },  

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.showLoading({
      title: '等待中',
      mask: true
    })
    app.request("daijiao/insured_person",{},true).then(res=>{
      wx.hideLoading()
      if(res.error_code==0){
       res.data.forEach((item)=>{
         item.flag=false
          return item
        })
        this.setData({
          userList:res.data
        })
      }else{
        wx.showModal({
          content: res.error_message,
          showCancel:false
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: "社保/公积金代缴、公积金提取!",
      path: '/pages/home/home',
      imageUrl: 'https://head.image.jiajijie.com/dj/images/fiveshareBg.png',
      success: function (res) {
      },
      fail: function (res) {
        console.log(res)
      }
    }
  }
})